package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * The type Setmeal controller.
 */
@Api(tags = "套餐管理")
@RequestMapping("/admin/setmeal")
@RequiredArgsConstructor
@Slf4j
@RestController
public class SetmealController {

    private final SetmealService setmealService;

    /**
     * 添加套餐
     *
     * @param setmealDTO the setmeal dto
     * @return the result
     */
    @ApiOperation("添加套餐")
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result<?> create(@RequestBody SetmealDTO setmealDTO) {
        setmealService.create(setmealDTO);
        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @param setmealPageQueryDTO the setmeal page query dto
     * @return the result
     */
    @ApiOperation("菜品套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult> readPageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setmealService.readPageSetmeal(setmealPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 根据ids删除
     *
     * @param ids the ids
     * @return the result
     */
    @ApiOperation("根据ids删除")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<?> deleteByIds(@RequestParam List<Long> ids) {
        setmealService.deleteByIds(ids);
        return Result.success();
    }


    /**
     * 套餐起售、停售
     *
     * @param status the status
     * @param id     the id
     * @return the result
     */
    @ApiOperation("套餐起售、停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<?> status(@PathVariable Integer status, Long id) {
        setmealService.status(status, id);
        return Result.success();
    }


    /**
     * 更据id查询回显
     *
     * @param id the id
     * @return the result
     */
    @ApiOperation("更据id查询回显")
    @GetMapping("/{id}")
    public Result<SetmealVO> infoById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.infoById(id);
        return Result.success(setmealVO);
    }

    /**
     *  更新套餐
     *
     * @param setmealDTO the setmeal dto
     * @return the result
     */
    @ApiOperation("更新套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<?> updateById(@RequestBody SetmealDTO setmealDTO){
      setmealService.updateById(setmealDTO);
      return Result.success();
    }


}
